package com.dianrui.operation.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.activity.TrackPlanningActivity;
import com.dianrui.operation.bean.PlanningRecord;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlanningRecordListAdapter extends BaseAdapter {

    Context context;
    List<PlanningRecord> planningRecordList;


    public PlanningRecordListAdapter(Context context) {
        this.context = context;

    }

    public void refresh(List<PlanningRecord> planningRecordList) {
        this.planningRecordList = planningRecordList;
        notifyDataSetChanged();
    }




    @Override
    public int getCount() {
        return planningRecordList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_planninglist, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (planningRecordList != null && planningRecordList.size() > 0) {
            final PlanningRecord planningRecord = planningRecordList.get(position);
            holder.createTime.setText(planningRecord.create_time);
            holder.startAddress.setText(planningRecord.start_address);
            holder.endAddress.setText(planningRecord.end_address);
            holder.number.setText("车辆编号:" + planningRecord.number);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,TrackPlanningActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("dispatch_id",planningRecord.dispatch_id);
                    bundle.putString("create_time",planningRecord.create_time);
                    bundle.putString("number",planningRecord.number);
                    bundle.putString("end_time",planningRecord.end_time);
                    bundle.putString("start_address",planningRecord.start_address);
                    bundle.putDouble("start_longitude",planningRecord.start_longitude);
                    bundle.putDouble("start_latitude",planningRecord.start_latitude);
                    bundle.putString("end_address",planningRecord.end_address);
                    bundle.putDouble("end_longitude",planningRecord.end_longitude);
                    bundle.putDouble("end_latitude",planningRecord.end_latitude);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });

        }
        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.create_time)
        TextView createTime;
        @BindView(R.id.start_address)
        TextView startAddress;
        @BindView(R.id.end_address)
        TextView endAddress;
        @BindView(R.id.number)
        TextView number;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
